﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uk.ac.soton.ses.DAL;
using System.IO;

namespace uk.ac.soton.ses
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            //Test2();
            Console.WriteLine("Finished.");
            Console.ReadLine();
        }

        static void Test1()
        {
            using (MaterialsMetadataContext dbcontext = new MaterialsMetadataContext(HDCConfiguration.Instance.ConfigurationOptions.DatabaseOptions.EntityConnectionString))
            {
                int ctscanType = 2;
                var datafiles =
                    // Retrieve datasets filtered by type
                    dbcontext.Experiments//.Where(ds => ds.ExperimentType.ID == ctscanType)
                    // Retrieve all files in the datasets
                    .SelectMany(ex => ex.ExperimentDataFiles)
                    // Filter the files by name
                    .Where(df => df.Filename.Contains(".xtekct"));
                    //Create a list of data files
                    //.ToList();
                
                foreach (ExperimentDataFile df in datafiles)
                {
                    try
                    {
                        df.ExperimentReference.Load();
                        FileParser_XTEKCT filedata = new FileParser_XTEKCT(df.GetFullPath());
                        foreach (KeyValuePair<string, string> pair in filedata.Properties)
                        {
                            Console.WriteLine(df.Experiment.ID + ":" + pair.Key + ":" + pair.Value);
                        }
                    }
                    catch (FileNotFoundException)
                    {
                    }
                    catch (InvalidDataException)
                    {
                    }
                }
            }
        }

        static void Test2()
        {
            using (MaterialsMetadataContext dbcontext = new MaterialsMetadataContext(HDCConfiguration.Instance.ConfigurationOptions.DatabaseOptions.EntityConnectionString))
            {
                var dataset16 = dbcontext.GetExperimentByID(16);
                
                int fatigueTestDSType = 15;
                var fatigueTests = dbcontext.Experiments.Where(ds => ds.ExperimentType.ID == fatigueTestDSType);
                //var related = dbcontext.Experiments.Select(exp=>exp.ExperimentChildren).
                //dataset16.ExperimentParents.Select(x=>
                //IEnumerable<Experiment> materials = dbcontext.GetExperimentsWithMatchingParameter("Composition");
                //int materialDSType = 1;
                //var materials = dbcontext.Experiments.Where(ds => ds.ExperimentType.ID == materialDSType);
                foreach (Experiment dataset in dbcontext.GetExperimentsWithMatchingParameter("Composition"))
                {
                    var compositionParameter = dataset.LookupParameters("Composition").FirstOrDefault();
                    if (compositionParameter == null) continue;
                    var chromiumParameter = compositionParameter.GetChildParameters("Cr").FirstOrDefault();
                    if (chromiumParameter == null || !chromiumParameter.ValueIsValid) continue;
                    float chromiumValue;
                    if (float.TryParse(chromiumParameter.Value, out chromiumValue) && chromiumValue > 10)
                    {
                        Console.WriteLine("Material '{0}' has {1} {2} chromium", dataset.Name, chromiumParameter.Value, chromiumParameter.Unit);
                    }
                }
            }
        }
    }
}
